if (!window.Oval) {var Oval = {};}

/*
  oConfigs: {
    inputId: '', 
    containerId: '', 
    dataLoaderIframeId: 'dataLoaderIframe', 
    dataLoaderPagePath: '../zmclspellcode/spellCodeDataLoader.jsp', 
    queryStrName: 'querystr', 
    queryTypeName: 'querytype', 
    queryType: '', 
    minQueryStrLength: 7, 
    delayTime: 500, 
    itemPropertyNames: [], 
    formatResult: function(itemProperties, queryString) {
    	...
    	return sResult;
    }, 
    selectionChangeEventHandler: function(arg0, arg1...) {
    	...
    }, 
    selectionChangeEventArguments: [arg0, arg1...],
    selectNamesNeedToHide: []
  }
*/
Oval.SPACS = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";	
Oval.SpellCodeAutoComplete = function(oConfigs) {
	
	//????????id
	this.inputId = null;
	
	//?????????div?id
	this.containerId = null;

	//?????iframe
	this.dataLoaderIframeId = 'dataLoaderIframe';
	
	//??????????
	this.dataLoaderPagePath = 'spellCodeDataLoader.jsp';
	
	this.maxResultsDisplayed = 10;
	
	//???????
	this.queryStrName = 'querystr';
	
	//??????????? add by tendyhu 09/03/17
	this.queryExtendStrArr = [];
	
	//???????????
	this.minQueryStrLength = 7;
	
	//???????????                     
	this.minQueryStrLengthArr = [];  
	
	//???????
	this.queryTypeName = 'querytype';
	
	//????
	this.queryType = '';
	
	//????????
	this.delayTime = 500;
	
	//???????key???
	this.itemPropertyNames = [];
	
	//?????????????????itemProperties???itemPropertyNames????????????String?????
	this.formatResult = function(itemProperties, queryString) {
  	return (oResultItem);
  };
  
  //?????????????
  this.selectionChangeEventHandler = null;
  
  //????????????????
  this.selectionChangeEventArguments = [];
  
  //????????????select?id
  this.selectNamesNeedToHide = [];
	
  if (oConfigs) {
    for (var sConfig in oConfigs) {
      this[sConfig] = oConfigs[sConfig];
    }
  }
  
  this._queryForResult = new YAHOO.widget.DS_JSFunction(function(sQuery) {
    if (sQuery == Oval.SpellCodeAutoComplete._noneValue) {
      return [];
    } else {
    	var objData = Oval.SpellCodeAutoComplete.spellCodeTmpData;
    	var arrData = [];
    	for (var i = 0; i < objData.length; i++) {
    		var obj = objData[i];
    		var arr = [];
    		var self = Oval.SpellCodeAutoComplete.currentSpellCodeAutoComplete;
    		for (var j = 0; j < self.itemPropertyNames.length; j++) {
    			arr.push(obj[self.itemPropertyNames[j]]);
    		}
    		arrData.push(arr);
    	}
      return arrData;
    }
  });
  
  this._doBeforeSendQuery = function(sQuery) {
    if (Oval.SpellCodeAutoComplete.needToQuery) {
      return sQuery;
    } else {
      return Oval.SpellCodeAutoComplete._noneValue;
    }
  };
  
 	
  //modified by tendyhu at date 09/03/17
  this.loadRequestPage = function() {
		var iframe = document.getElementById(this.dataLoaderIframeId);			
		  	
		if(this.queryExtendStrArr.length == 0){
			iframe.src = this.dataLoaderPagePath + '?' + this.queryStrName + '=' + escape(this.queryStr) 
			+ '&' + this.queryTypeName + '=' +escape(this.queryType);	
		}else{
			var arr = this.queryExtendStrArr;			
			var compStr = "";
		    for(i in arr){	
		    		compStr+=('&'+arr[i].key+'='+arr[i].value);		    	
		    }		    
		  iframe.src = this.dataLoaderPagePath + '?' + this.queryStrName + '=' + escape(this.queryStr) 
			+ '&' + this.queryTypeName + '=' +escape(this.queryType)+compStr;  			
		}		
		
	};
  
  this._textboxKeyHandle = function() {
  	var ac = arguments[1][0];
  	if (Oval.SpellCodeAutoComplete._nDelayID != -1) {
  		clearTimeout(Oval.SpellCodeAutoComplete._nDelayID);
  	}
  	var queryStr = ac._oTextbox.value;
  	var self = arguments[2];
  	self.queryStr = queryStr;
  	
  	//var flag = false;
  	if (self.queryExtendStrArr.length != 0){
  		this.queryExtendStrArr = self.queryExtendStrArr;
  		//flag = true;
  	}
  	
  	Oval.SpellCodeAutoComplete.currentSpellCodeAutoComplete = self;
  	if (self.minQueryStrLengthArr.length == 0){
  	    if (queryStr && queryStr != '' && queryStr.length >= self.minQueryStrLength) {
  		    Oval.SpellCodeAutoComplete._nDelayID = setTimeout(function() {  		    
  		    		self.loadRequestPage();	  		    
  		    }, self.delayTime);
  	    }
  	}else{
  	    var arr = self.minQueryStrLengthArr;  	  	
	  	if (queryStr && queryStr != '') {
		    for(i in arr){	
				if (queryStr.substring(0,1)==(arr[i].key) && (queryStr.length) >= (arr[i].value)) {
					Oval.SpellCodeAutoComplete._nDelayID = setTimeout(function() {	self.loadRequestPage();
	  				}, self.delayTime);				 				
				}
			}
		}
  	}
  };
  
	this._itemSelectHandle = function() {
		var spellCodeAC = Oval.SpellCodeAutoComplete.currentSpellCodeAutoComplete;
		spellCodeAC.selectionChangeEventHandler.call(spellCodeAC, arguments[1][2]);
	};

	this._selectionEnforcehandle = function() {
		var spellCodeAC = Oval.SpellCodeAutoComplete.currentSpellCodeAutoComplete;
		this._oTextbox.value = '';
		spellCodeAC.selectionChangeEventHandler.apply(spellCodeAC);
	};
	
	this._hideSelects = function(visibility){
		for(i = 0; i < this.selectNamesNeedToHide.length; i++) {
			var select = document.getElementsByName(this.selectNamesNeedToHide[i])[0];
			if (select) {
				select.style.visibility = visibility;
			}
		}
	};
	
	this._containerExpandHander = function() {
		var spellCodeAC = arguments[2];
		spellCodeAC._hideSelects('hidden');
	};
	
	this._containerCollapseHander = function() {
		var spellCodeAC = arguments[2];
		spellCodeAC._hideSelects('visible');
	};

  if (this.inputId && this.containerId) {
    this.autoComplete = new YAHOO.widget.AutoComplete(this.inputId, this.containerId, this._queryForResult);
    var inputValue = document.getElementById(this.inputId).value;
    if (inputValue && inputValue != '') {
    	this.autoComplete._bItemSelected = true;
    }
    this.autoComplete.forceSelection = true;
    this.autoComplete.formatResult = this.formatResult;
    this.autoComplete.maxResultsDisplayed = this.maxResultsDisplayed;
    this.autoComplete.doBeforeSendQuery = this._doBeforeSendQuery;
    this.autoComplete.textboxKeyEvent.subscribe(this._textboxKeyHandle, this);
    if (this.selectNamesNeedToHide && this.selectNamesNeedToHide.length != 0) {
    	this.autoComplete.containerExpandEvent.subscribe(this._containerExpandHander, this);
    	this.autoComplete.containerCollapseEvent.subscribe(this._containerCollapseHander, this);
    }
    if (this.selectionChangeEventHandler && this.selectionChangeEventHandler instanceof Function) {
			this.autoComplete.itemSelectEvent.subscribe(this._itemSelectHandle, this);
			this.autoComplete.selectionEnforceEvent.subscribe(this._selectionEnforcehandle, this);
    }
        
    var onInputKeyDown = document.getElementById(this.inputId).onkeydown;
    var input = document.getElementById(this.inputId);
    var removeDefaultKeyDown = function() {
    	input.onkeydown = null;
    };
    var addDefaultKeyDown = function() {
    	input.onkeydown = onInputKeyDown;
    }
    
    this.autoComplete.containerExpandEvent.subscribe(removeDefaultKeyDown, this);
    this.autoComplete.containerCollapseEvent.subscribe(addDefaultKeyDown, this);
    

  }
};

Oval.SpellCodeAutoComplete._nDelayID = -1;

Oval.SpellCodeAutoComplete._noneValue = 'noneValue';

//??????
Oval.SpellCodeAutoComplete.spellCodeTmpData = [];

//???????????
Oval.SpellCodeAutoComplete.needToQuery = false;

//?????????SpellCodeAutoComplete??
Oval.SpellCodeAutoComplete.currentSpellCodeAutoComplete = null;
